package com.awesome.media;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.net.Uri;
import android.util.Log;
import android.view.Surface;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2020/4/29 14:24
 * Description:Android 原生播放器
 */
public class JeejioMediaPlayer extends AbsPlayer {
    private final MediaPlayer mMediaPlayer;

    public JeejioMediaPlayer(Context context) {
        super(context);
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.i(TAG, "onPrepared");
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onPrepared();
                }
            }
        });
        mMediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                Log.i(TAG, "onInfo" + " : "
                        + "what--->" + what
                        + ",extra--->" + extra);
                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onVideoRenderingStart();
                    }
                }
                if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onBufferingStart();
                    }
                }
                if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onBufferingEnd();
                    }
                }
                return false;
            }
        });
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.e(TAG, "onError" + " : "
                        + "what--->" + what
                        + ",extra--->" + extra);
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onError(new Exception("播放失败"));
                }
                return true;
            }
        });
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.i(TAG, "onCompletion");
                mMediaPlayer.reset();
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onComplete();
                }
            }
        });
        mMediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.i(TAG, "onBufferingUpdate" + " : "
                        + "percent--->" + percent);
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onBufferingUpdate(percent);
                }
            }
        });
        mMediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                Log.i(TAG, "onVideoSizeChanged" + " : "
                        + "width--->" + width
                        + ",height--->" + height);
                if (mMediaPlayerListener != null) {
                    mMediaPlayerListener.onVideoSizeChanged(width, height);
                }
            }
        });
    }

    @Override
    public void setDisplay(Surface surface) {
        mMediaPlayer.setSurface(mSurface = surface);
    }

    @Override
    public void play(Uri uri) {
        if (mScheduledFuture != null
                && !mScheduledFuture.isDone()
                && !mScheduledFuture.isCancelled()) {
            mScheduledFuture.cancel(true);
            mScheduledFuture = null;
        }
        mScheduledFuture = mScheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                mMediaPlayer.reset();
                mMediaPlayer.setSurface(mSurface);
                try {
                    mMediaPlayer.setDataSource(mContext, uri);
                } catch (IOException e) {
                    if (mMediaPlayerListener != null) {
                        mMediaPlayerListener.onError(e);
                    }
                    return;
                }
                mMediaPlayer.prepareAsync();
            }
        }, 100, TimeUnit.MILLISECONDS);
    }

    @Override
    public void start() {
        mMediaPlayer.start();
    }

    @Override
    public void pause() {
        mMediaPlayer.pause();
    }

    @Override
    public void stop() {
        mMediaPlayer.stop();
    }

    @Override
    public void reset() {
        mMediaPlayer.reset();
    }

    @Override
    public void release() {
        mMediaPlayer.release();
    }

    @Override
    public void seekTo(long position) {
        mMediaPlayer.seekTo((int) position);
    }

    @Override
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    @Override
    public long getCurrentPosition() {
        return mMediaPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return mMediaPlayer.getDuration();
    }

    @Override
    public int getAudioSessionId() {
        return mMediaPlayer == null ? -1 : mMediaPlayer.getAudioSessionId();
    }

    @Override
    public int getVideoWidth() {
        return mMediaPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return mMediaPlayer.getVideoHeight();
    }

    @Override
    public void setVolume(float volume) {
        mMediaPlayer.setVolume(volume, volume);
    }

    @Override
    public void setSpeed(float speed) {
        // MediaPlayer 只有 6.0 以上支持
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            PlaybackParams playbackParams = mMediaPlayer.getPlaybackParams();
            playbackParams.setSpeed(speed);
            mMediaPlayer.setPlaybackParams(playbackParams);
        }
    }

    @Override
    public float getSpeed() {
        // MediaPlayer 只有 6.0 以上支持
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            return mMediaPlayer.getPlaybackParams().getSpeed();
        }
        return 1;
    }

    public MediaPlayer getMediaPlayer() {
        return mMediaPlayer;
    }
}